﻿<?xml version="1.0" encoding="utf-8" ?>
<content>
	<mainContent>
		<![CDATA[
		<h2>Http Error Handling <span>Search Engines will Love You!</span></h2>
		<p>One of the major problems with .Net error handling is that it returns invalid responses to search engines
			for common operations such as <strong>404 missing pages</strong> and <strong>server errors</strong>. Take the following configuration, for example:</p>
		<pre class="csharpcode">
&lt;customErrors mode=<span class="str">"on"</span> defaultRedirect="<span class="str">"~/error.htm"</span>&gt;
  &lt;error statusCode=<span class="str">"404"</span> redirect="<span class="str">"~/page-not-found.htm"</span>/&gt;
&lt;/customErrors&gt;
		</pre>
		<p>If you request a url from a website that cannot be found, you will receive a <strong>301</strong> temporary redirect and then a <strong>200</strong> 
			success, which is the success of the actual 404 page being returned to the browser. This is very bad
			from an SEO perspective as search engines will potentially index the content of the 404 page.</p>
		<p>What we need is a 404 response immediately upon requesting a missing page.</p>
		<p>DNAide can handle this for us!</p>
		<pre class="csharpcode">
&lt;dnAide&gt;
  &lt;web&gt;
    &lt;httpErrorModule enabled="true"&gt;
      &lt;rules&gt;
        &lt;add code=<span class="str">"404"</span> handlerPage=<span class="str">"~/Templates/Errors/404.aspx"</span> /&gt;
        &lt;add code=<span class="str">"500"</span> handlerPage=<span class="str">"~/Templates/Errors/500.aspx"</span> /&gt;
      &lt;/rules&gt;
    &lt;/httpErrorModule&gt;
  &lt;/web&gt;
&lt;/dnAide&gt;
		</pre>
		<p>Used in conjunction with our error IHttpModule.</p>
		<pre class="csharpcode">
&lt;httpModules&gt;
  &lt;add type=<span class="str">"DNAide.Web.UrlRewriter"</span> name=<span class="str">"UrlRewriter"</span> /&gt;
  &lt;add type=<span class="str">"DNAide.Web.HttpErrorModule"</span> name=<span class="str">"HttpErrorModule"</span> /&gt;
&lt;/httpModules&gt;
		</pre>
		<p>The error web forms exist as physical pages in the website, which we feel gives you greater
			control over their layout and reduces the risk of an error being thrown by an error page, as they are just static HTML!</p>
		<p>In their code behinds, we set the Http status code to 404, which you can see in action from <a href="http://www.fiddler2.com" class="newwindow">Fiddler</a>.</p>
		<p>Try it.</p>
		<p><a href="seo-friendly-error-handling/you-will-not-find-me.htm">You'll not find me!</a><br /><a href="seo-friendly-error-handling/throw.htm">Throw an error!</a></p>
		<h3>Next Steps:</h3>
		<p>You can build on our simple error catching module by overriding the HandleError method and pointing
			your web.config to your own implementation.</p>
		<pre class="csharpcode">
...

<span class="kwrd">protected override bool</span> HandleError(<span class="kwrd">int</span> code, <span class="kwrd">HttpException</span> error)
{
  <span class="kwrd">if</span> (code == 500)
  {
    <span class="rem">// Log error to an email address or central bug tracking system</span>
  }

  <span class="kwrd">return true</span>;
}

...
		</pre>
		<p>This opens up a world of options for all your error tracking needs, whilst keeping the search engines happy!</p>
		]]>
	</mainContent>

	<secondaryContent>
		<![CDATA[
		]]>
	</secondaryContent>
</content>